#include <iostream>
#include <unistd.h>
#include <cstring>

int main(int argc, char *argv[])
{
    for (int i = 0; argv[i]; i++)
        std::cout << "atgv[" << i << "]: " << argv[i] << std::endl;
    for (int i = 0; argc; i++)
        std::cout << "atgv[" << i << "]: " << argv[i] << std::endl;
    return 0;
}

/*
//要实现三种不同的功能
// ./myprocess -3
// ./myprocess -add 1 2
int main(int argc, char *argv[])
{
    if (argc != 4)
    {
        std::cout << "Usage:\n\t" << argv[0] << " -[add | sub | mul | div] x y\n\n";
        return 1;
    }

    int x = atoi(argv[2]), y = atoi(argv[3]);

    if (strcmp("-add", argv[1]) == 0)
        std::cout << x << " + " << y << " = " << x + y << std::endl;
    else if (strcmp("-sub", argv[1]) == 0)
        std::cout << x << " - " << y << " = " << x - y << std::endl;
    else if (strcmp("-mul", argv[1]) == 0)
        std::cout << x << " / " << y << " = " << x / y << std::endl;
    else if (strcmp("-div", argv[1]) == 0)
        std::cout << x << " * " << y << " = " << x * y << std::endl;
    else
        std::cout << "Unkown!\n";

    return 0;
}


int main(int argc, char *argv[], char *env[])
{
    std::cout << "I am a process, pid: " << getpid() << ", ppid: " << getppid() << std::endl;
    for (int i = 0; env[i]; i++)
        std::cout << "-------------------------env[" << i << "] -> " << env[i] << std::endl;

    pid_t id = fork();
    if (id == 0)
    {
        std::cout << "-----------------------------------------" << std::endl;
        std::cout << "I am child, pid: " << getpid() << ", ppid: " << getppid() << std::endl;
        for (int i = 0; env[i]; i++)
            std::cout << "-------------------------env[" << i << "] -> " << env[i] << std::endl;
    }
    return 0;
}



#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
int main()
{
    const char *username = getenv("USER");
    if (strcmp(username, "sheep") == 0 || strcmp(username, "xz") == 0)
        std::cout << "this is my process core function" << std::endl;
    else
        std::cout << "你没有权限" << std::endl;

    return 0;
}

int main()
{
    pid_t id = fork();
    if (id == 0)
    {
        fork();
        // child
        int cnt = 10;
        while (cnt--)
        {
            std::cout << "I am child, pid: " << getpid() << ", ppid: " << getppid()
                      << ", cnt: " << cnt << std::endl;
            sleep(1);
        }
        exit(0); // 让子进程直接退出
    }

    int cnt = 5;
    // father
    while (cnt--)
    {
        std::cout << "I am father, pid: " << getpid() << ", ppid: " << getppid()
                  << ", cnt: " << cnt << std::endl;
        sleep(1);
    }

    wait(nullptr);
    std::cout << "father wait child done..." << std::endl;
    sleep(5);


    return 0;
}

const int num = 10;

void Worker()
{
    int cnt = 12;
    while (cnt--)
    {
        std::cout << "child " << getpid() << " is running, cnt: " << cnt << std::endl;
        sleep(1);
    }
}
int main()
{
    for (int i = 0; i < num; i++)
    {
        pid_t id = fork();
        if (id < 0)
            break;
        if (id == 0)
        {
            // 子进程
            Worker();
            exit(0);
        }
        std::cout << "father create child process success, child pid: " << id <<std::endl;
        sleep(1);
    }
    sleep(15);
    return 0;
}
*/